package com.iflytek.application.common.exception;

import com.iflytek.application.common.constant.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by guanxf
 */
@ControllerAdvice
public class DefaultExceptionHandler {
     Logger log  =  LoggerFactory.getLogger(DefaultExceptionHandler.class);
    @ExceptionHandler(ContextRuntimeException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Object handleContextRuntimeException(ContextRuntimeException exception) {
        log.error(exception.getRawMessage(), exception);
        return exception.getContextEntries();
    }

    @ExceptionHandler(DataAccessException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Object handleDBException(DataAccessException throwable) {
        log.error(throwable.getMessage(), throwable);
        return buildResultMap(ErrorCode.COMM_DATA_ACCESS, "系统异常");
    }

    @ExceptionHandler(TypeMismatchException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Object handleInvalidRequestException(TypeMismatchException throwable) {
        log.error(throwable.getMessage(), throwable);
        return buildResultMap(ErrorCode.COMM_PARAMETER_MISMATCH, "参数类型不匹配");
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Object handleInvalidRequestException(MethodArgumentNotValidException throwable) {
        log.error(throwable.getMessage(), throwable);
        return buildResultMap(ErrorCode.COMM_PARAMETER_INVALID, "方法参数无效");
    }

    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Object handleUnexpectedTypeException(Throwable throwable) {
        log.error(throwable.getMessage(), throwable);
        return buildResultMap(ErrorCode.COMM_READ_REQEUST_VALUE_ERROR,  "参数值不合法");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Object handleInvalidRequestException(HttpMessageNotReadableException throwable) {
        log.error(throwable.getMessage(), throwable);
        return buildResultMap(ErrorCode.COMM_READ_REQEUST_VALUE_ERROR,  "参数值不合法");
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Object handleInvalidRequestException(IllegalArgumentException throwable) {
        log.error(throwable.getMessage(), throwable);
        return buildResultMap(ErrorCode.COMM_READ_REQEUST_VALUE_ERROR, "参数值不合法");
    }

    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Object handleOtherException(Throwable throwable) {
        log.error(throwable.getMessage(), throwable);
        return buildResultMap(ErrorCode.COMM_UNKNOWN,"未知错误");
    }



    private Object buildResultMap(String status, String message) {
         Map<String, Object> result = new HashMap<String, Object>();
         result.put("status", status);
         result.put("time", new Date());
         result.put("message", message);
        return result;
    }

}
